/**
 * 
 */
package com.cpi.cornpp.common.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import com.cs.common.init.SiteMapCollection;


/**
 * Security Filter
 * @author Administrador
 */

public class BreadCrumbFilter implements Filter {

	/** */
	private static final String BREADCRUMBS = "BREADCRUMBS";
	private static final String BREADCRUMB_LABEL = "BREADCRUMB_LABEL";
	/** Init filter config */
	private FilterConfig filterConfig = null;
	/**
	 * @see javax.servlet.Filter#destroy()
	 */
	public void destroy() {
		filterConfig = null;
	}
	/** 
	 * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
	 */
	public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
		
		HttpServletRequest httpServletRequest = null;		
		httpServletRequest = (HttpServletRequest)servletRequest;
		HttpServletRequest request = (HttpServletRequest)servletRequest;
		String requestedServlet = httpServletRequest.getServletPath();
		try{
			BreadCrumbList breadcrumbs = SiteMapCollection.getInstance().getBreadCrumbsFor(requestedServlet);
			if(breadcrumbs!=null){
				if(breadcrumbs.getSameAs()==null || breadcrumbs.getSameAs().trim().equals("")){
					;
				}else{
					breadcrumbs = SiteMapCollection.getInstance().getBreadCrumbsFor(breadcrumbs.getSameAs());
				}	
				request.getSession().setAttribute(BREADCRUMBS, breadcrumbs);
				request.getSession().setAttribute(BREADCRUMB_LABEL, breadcrumbs.getLabel());
			}
		}catch(Exception ex){
			ex.printStackTrace();
		}
		filterChain.doFilter(servletRequest, servletResponse);

	}
	/**
	 * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
	 */
	public void init(FilterConfig nFilterConfig) throws ServletException {
		filterConfig = nFilterConfig;		
		System.out.println(filterConfig);
	}
}
